Completed
Push — master ( f2cb58...56e3cd )
by Rafael S.
01:56
created

integer.js ➔ setLastByteMask_   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 3
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 5
rs 10
1
/*
2
 * Copyright (c) 2017-2018 Rafael da Silva Rocha.
3
 *
4
 * Permission is hereby granted, free of charge, to any person obtaining
5
 * a copy of this software and associated documentation files (the
6
 * "Software"), to deal in the Software without restriction, including
7
 * without limitation the rights to use, copy, modify, merge, publish,
8
 * distribute, sublicense, and/or sell copies of the Software, and to
9
 * permit persons to whom the Software is furnished to do so, subject to
10
 * the following conditions:
11
 *
12
 * The above copyright notice and this permission notice shall be
13
 * included in all copies or substantial portions of the Software.
14
 *
15
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19
 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20
 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
 *
23
 */
24
25
/**
26
 * @fileoverview Pack and unpack two's complement ints and unsigned ints.
27
 * @see https://github.com/rochars/byte-data
28
 */
29
30
/**
31
 * A class to pack and unpack two's complement ints and unsigned ints.
32
 */
33
export default class Integer {
34
35
  /**
36
   * @param {number} bits Number of bits used by the data.
37
   * @param {boolean} signed True for signed types.
38
   * @throws {Error} if the number of bits is smaller than 1 or greater than 64.
39
   */
40
  constructor(bits, signed) {
41
    /**
42
     * The max number of bits used by the data.
43
     * @type {number}
44
     * @private
45
     */
46
    this.bits_ = bits;
47
    /**
48
     * The number of bytes used by the data.
49
     * @type {number}
50
     * @private
51
     */
52
    this.offset_ = 0;
53
    /**
54
     * The practical number of bits used by the data.
55
     * @type {number}
56
     * @private
57
     */
58
    this.realBits_ = this.bits_;
59
    /**
60
     * The mask to be used in the last byte.
61
     * @type {number}
62
     * @private
63
     */
64
    this.lastByteMask_ = 255;
65
    /** @type {number} */
66
    let max = Math.pow(2, this.bits_);
67
    if (signed) {
68
      this.max_ = max / 2 -1;
69
      this.min_ = -max / 2;
70
    } else {
71
      this.max_ = max - 1;
72
      this.min_ = 0;
73
    }
74
    this.build_();
75
  }
76
77
  /**
78
   * Read one integer number from a byte buffer.
79
   * @param {!Uint8Array} bytes An array of bytes.
80
   * @param {number=} i The index to read.
81
   * @return {number}
82
   */
83
  read(bytes, i=0) {
84
    /** @type {number} */
85
    let num = 0;
86
    /** @type {number} */
87
    let x = this.offset_ - 1;
88
    for (; x > 0; x--) {
89
      num = (bytes[x + i] << x * 8) | num;
90
    }
91
    num = (bytes[i] | num) >>> 0;
92
    return this.overflow_(this.sign_(num));
93
  }
94
95
  /**
96
   * Write one integer number to a byte buffer.
97
   * @param {!Array<number>} bytes An array of bytes.
98
   * @param {number} num The number.
99
   * @param {number=} j The index being written in the byte buffer.
100
   * @return {number} The next index to write on the byte buffer.
101
   */
102
  write(bytes, num, j=0) {
103
    bytes[j] = this.overflow_(num) & 255;
104
    j++;
105
    for (let i = 2; i <= this.offset_; i++, j++) {
106
      bytes[j] = Math.floor(num / Math.pow(2, ((i - 1) * 8))) & 255;
107
    }
108
    return j;
109
  }
110
111
  /**
112
   * Write one integer number to a byte buffer.
113
   * @param {!Array<number>} bytes An array of bytes.
114
   * @param {number} num The number.
115
   * @param {number=} j The index being written in the byte buffer.
116
   * @return {number} The next index to write on the byte buffer.
117
   * @private
118
   */
119
  writeEsoteric_(bytes, num, j=0) {
120
    j = this.writeFirstByte_(bytes, this.overflow_(num), j);
121
    for (let i = 2; i < this.offset_; i++, j++) {
122
      bytes[j] = Math.floor(num / Math.pow(2, ((i - 1) * 8))) & 255;
123
    }
124
    if (this.bits_ > 8) {
125
      bytes[j] = Math.floor(
126
          num / Math.pow(2, ((this.offset_ - 1) * 8))) & this.lastByteMask_;
127
      j++;
0 ignored issues
show
Complexity Coding Style introduced by
You seem to be assigning a new value to the loop variable j here. Please check if this was indeed your intention. Even if it was, consider using another kind of loop instead.
Loading history...
128
    }
129
    return j;
130
  }
131
132
  /**
133
   * Read a integer number from a byte buffer by turning int bytes
134
   * to a string of bits. Used for data with more than 32 bits.
135
   * @param {!Uint8Array} bytes An array of bytes.
136
   * @param {number=} i The index to read.
137
   * @return {number}
138
   * @private
139
   */
140
  readBits_(bytes, i=0) {
141
    /** @type {string} */
142
    let binary = '';
143
    for (let j = 0; j < this.offset_; j++) {
144
      /** @type {string} */
145
      let bits = bytes[i + j].toString(2);
146
      binary = new Array(9 - bits.length).join('0') + bits + binary;
147
    }
148
    return this.overflow_(this.sign_(parseInt(binary, 2)));
149
  }
150
151
  /**
152
   * Build the type.
153
   * @throws {Error} if the number of bits is smaller than 1 or greater than 64.
154
   * @private
155
   */
156
  build_() {
157
    this.setRealBits_();
158
    this.setLastByteMask_();
159
    this.offset_ = this.bits_ < 8 ? 1 : Math.ceil(this.realBits_ / 8);
160
    if ((this.realBits_ != this.bits_) || this.bits_ < 8 || this.bits_ > 32) {
161
      this.write = this.writeEsoteric_;
162
      this.read = this.readBits_;
163
    }
164
  }
165
166
  /**
167
   * Sign a number.
168
   * @param {number} num The number.
169
   * @return {number}
170
   * @private
171
   */
172
  sign_(num) {
173
    if (num > this.max_) {
174
      num -= (this.max_ * 2) + 2;
175
    }
176
    return num;
177
  }
178
179
  /**
180
   * Trows error in case of underflow or overflow.
181
   * @param {number} num The number.
182
   * @return {number}
183
   * @throws {Error} on overflow or underflow.
184
   * @private
185
   */
186
  overflow_(num) {
187
    if (num > this.max_) {
188
      throw new Error('Overflow.');
189
    } else if (num < this.min_) {
190
      throw new Error('Underflow.');
191
    }
192
    return num;
193
  }
194
195
  /**
196
   * Set the practical bit number for data with bit count different
197
   * from the standard types (8, 16, 32, 40, 48, 64).
198
   * @private
199
   */
200
  setRealBits_() {
201
    this.realBits_ = ((this.bits_ - 1) | 7) + 1;
202
  }
203
204
  /**
205
   * Set the mask that should be used when writing the last byte.
206
   * @private
207
   */
208
  setLastByteMask_() {
209
    /** @type {number} */
210
    let r = 8 - (this.realBits_ - this.bits_);
211
    this.lastByteMask_ = Math.pow(2, r > 0 ? r : 8) - 1;
212
  }
213
214
  /**
215
   * Write the first byte of a integer number.
216
   * @param {!Array<number>} bytes An array of bytes.
217
   * @param {number} number The number.
218
   * @param {number} j The index being written in the byte buffer.
219
   * @return {number} The next index to write on the byte buffer.
220
   * @private
221
   */
222
  writeFirstByte_(bytes, number, j) {
223
    if (this.bits_ < 8) {
224
      bytes[j] = number < 0 ? number + Math.pow(2, this.bits_) : number;
225
    } else {
226
      bytes[j] = number & 255;
227
    }
228
    return j + 1;
229
  }
230
}
231